package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.AccdentdetailMapper;
import cn.wolfcode.crm.mapper.SclaimMapper;
import cn.wolfcode.crm.query.AccentdatailQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAccdentdetailService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AccdentdetailServiceImpl implements IAccdentdetailService {

    @Autowired
    private AccdentdetailMapper accdentdetailMapper;
    @Autowired
    private SclaimMapper sclaimMapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        accdentdetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Accdentdetail selectByPrimaryKey(Long id) {
        return accdentdetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Accdentdetail> selectAll() {
        return accdentdetailMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Accdentdetail entity) {
        if (entity.getId() != null){
            accdentdetailMapper.updateByPrimaryKey(entity);
        }else {
            //entity.setState(true);
            accdentdetailMapper.insert(entity);
        }
    }

    public void changeAuditState(Long id) {
        accdentdetailMapper.changeAuditState(id);
    }

    @Override
    public void changeState(Long id,Guard guard, Claim claim) {
        accdentdetailMapper.changeState(id);
        Sclaim sclaim = new Sclaim();
        sclaim.setTotalamount(claim.getAmount());
        sclaim.setClient(guard.getClient());
        sclaim.setProduct(guard.getProduct());
        sclaim.setPromise(guard.getProduct().getPromise());
        sclaim.setClaim_date(new Date());
        sclaimMapper.insert(sclaim);
    }

    @Override
    public void updateDate(Long id) {
        Date date = new Date();
        accdentdetailMapper.updateDate(id,date);
    }

    public void updateClaim(Long id) {
        accdentdetailMapper.updateClaim(id);
    }

    public void insertGuardId(Long id, Long guardId) {
        accdentdetailMapper.insertGuardId(id, guardId);
    }


    @Override
    public PageResult query(QueryObject qo) {
        int total = accdentdetailMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Accdentdetail> rows = accdentdetailMapper.query4List(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void insertCase(Report entity) {
        Accdentdetail accdentdetail = new Accdentdetail();
        accdentdetail.setCarnumber(entity.getCarnumber());
        accdentdetail.setHappenplace(entity.getPlace());
        accdentdetail.setTel(entity.getTel());
        accdentdetail.setGuardNum(entity.getGetguard());
        accdentdetail.setState(0L);
        accdentdetail.setAcceptDate(new Date());
        accdentdetail.setTelname(entity.getName());
        if (entity.getId() != null){
            accdentdetailMapper.updateByPrimaryKey(accdentdetail);
        }else {
            //entity.setState(true);
            accdentdetailMapper.insert(accdentdetail);
        }
    }

    @Override
    public PageResult auditquery(AccentdatailQueryObject qo) {
        int total = accdentdetailMapper.auditquery4Count();
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Accdentdetail> rows = accdentdetailMapper.auditquery(qo);
        return new PageResult(total,rows);
    }

}
